package com.atguigu.srb.base.exception;

import com.atguigu.srb.base.response.R;
import com.atguigu.srb.base.response.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

@Slf4j
// 全局异常处理  处理后的结果转为json响应
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public R exception(Exception e){ //形参类型需要和异常处理注解配置的类型一致
        //打印异常的堆栈消息(包含了代码那些行出现了什么问题)
        log.error("出现了异常: {}", ExceptionUtils.getStackTrace(e));
        return R.error();
    }
    //希望给前端一个比较确定的提示
    @ExceptionHandler(value = BadSqlGrammarException.class)
    public R exception(BadSqlGrammarException e){ //形参类型需要和异常处理注解配置的类型一致
        //打印异常的堆栈消息(包含了代码那些行出现了什么问题)
        log.error("出现了异常: {}", ExceptionUtils.getStackTrace(e));
        return R.setResult(ResponseEnum.BAD_SQL_GRAMMAR_ERROR);
    }
//    @ExceptionHandler(value = HttpMessageNotReadableException.class)
//    public R exception(HttpMessageNotReadableException e){ //形参类型需要和异常处理注解配置的类型一致
//        //打印异常的堆栈消息(包含了代码那些行出现了什么问题)
//        log.error("出现了异常: {}", ExceptionUtils.getStackTrace(e));
//        return R.setResult(ResponseEnum.PARAM_ERROR);
//    }

    //希望根据异常类型返回一个提示比较明确的失败的R对象 给前端使用
    //但是每个异常都编写一个异常处理器 冗余较多
    //可以自定义异常，并扩展异常类(添加异常描述信息：失败code 失败msg)，再通过自定义异常的处理器
    // 接收到自定义异常后 根据code和msg创建失败的R对象返回
    @ExceptionHandler(value = BusinessException.class)
    public R exception(BusinessException e){ //形参类型需要和异常处理注解配置的类型一致
        //打印异常的堆栈消息(包含了代码那些行出现了什么问题)
        log.error("出现了异常: {}", ExceptionUtils.getStackTrace(e));
        return R.error().code(e.getCode()).msg(e.getMsg());
    }

    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public R handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return R.error().msg(ResponseEnum.SERVLET_ERROR.getMsg()).code(ResponseEnum.SERVLET_ERROR.getCode());
    }
}
